<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <link rel="stylesheet" href="css/index.css">
    
</head>
<body>
    <div class="content">
        <div id="list">
            <img src="images/1.jpg" alt="">
            <img src="images/2.jpg" alt="">
            <img src="images/3.jpg" alt="">
            <img src="images/4.jpg" alt="">
            <img src="images/5.jpg" alt="">
        </div>
        <button id="prev"></button>
        <button id="next"></button>
        <div id="btns">
            <span class="current"></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
    </div>

    <script>
        $(function () {
            var index = 0;
            $("#next").click(function () {
                index++;
                if (index > 4) {
                    index = 0;
                }
                console.log(index);
                $("#list img").eq(index).fadeIn().siblings().fadeOut();
                $("#btns span").eq(index).addClass("current").siblings().removeClass("current");
            })
            $("#prev").click(function () {
                index--;
                
                if (index < 0) {
                    index = $("#list img").length-1;
                }
                console.log(index);
                $("#list img").eq(index).fadeIn().siblings().fadeOut();
                $("#btns span").eq(index).addClass("current").siblings().removeClass("current");
            })
            $("#btns span").click(function(){
                let  i = $(this).index();
                console.log(i);
                // 点击焦点之后继续点右边的按钮，会出现图片乱跳的效果，
                // 就需要将外部的index重置与i相等，正好保证在点完焦点后继续点按钮不会出现乱序
                index = i;
                $("#list img").eq(i).fadeIn().siblings().fadeOut();
                $("#btns span").eq(i).addClass("current").siblings().removeClass("current");
            })
        })
    </script>
</body>
</html>